<?php
/***
 * nft
 */
namespace app\api\controller;

use app\api\controller\BaseController;

class NftController extends BaseController{
	/**
	*
    *nft列表
	**/
	public function getList(){
        $param		= input('post.');
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
        $token			= (input('post.token')) ? input('post.token') : '';
		$where = 'a.state=1 and a.is_move=0';
		if($token){
			$userArr	= explode(',',auth_code($token,'DECODE'));
			$uid		= $userArr[0];
			$where.=" and a.uid=$uid";

		}
		//每页记录数
		$pageSize	= (isset($param['pagesize']) and $param['pagesize']) ? $param['pagesize'] : 10;
		//当前页
		$page		= (isset($param['page']) and $param['page']) ? $param['page'] : 1;

        $data	= model('Nft')->alias('a')
            ->field('a.*')
            ->where($where)
            ->order('create_time desc')
            ->paginate($pageSize)
            ->toArray();

        $statusArr=model('Nft')->getStatus();
		foreach ($data['data'] as $key => &$value) {
			$value['file']=$value['file']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$value['file']):'';
            $value['voucher']=$value['voucher']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$value['voucher']):'';
            $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
            $value['statusStr']=$statusArr[$value['status']];
            $value['credit_type_str']= getCreditTypeName($value['credit_type']);
		}
		return json(['code'=>1,'data'=>$data]);
	}
    /**
	*
    *nft详情
	**/
	public function getDetail(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
		$id		= input('post.id');
        if(empty($id)){
            return json(['code'=>0,'code_dec'=>config($lang.'.lang2')[0]]);
        }
        $token			= (input('post.token')) ? input('post.token') : '';
		$where = 'a.status !=-1 ';
		if($token){
			$userArr	= explode(',',auth_code($token,'DECODE'));
			$uid		= $userArr[0];
			//$where.=" and a.uid=$uid";

		}

        $data	= model('Nft')->alias('a')
            ->field('a.*')
            ->where($where)
            ->find($id);
        if(empty($data)){
            return ['code'=>0,'code_dec'=>config($lang.'.lang2')[1]];
        }
        $statusArr=model('Nft')->getStatus();
		$data['file']=$data['file']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$data['file']):'';
        $data['voucher']=$data['voucher']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$data['voucher']):'';
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        $data['statusStr']=$statusArr[$data['status']];
        $data['credit_type_str']= getCreditTypeName($data['credit_type']);

		return json(['code'=>1,'data'=>$data]);
	}
	/**
	*
    *转入nft
	**/
	public function import(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
        $token			= request()->param('token','','trim') ;

        $param = input('param.');

        if (!isset($param['file'])||count($param['file'])<=0||!isset($param['voucher'])||count($param['voucher'])<=0) {
            return ['code'=>0,'code_dec'=>config($lang.'.lang2')[2]];
        }

        $data['uid']=0;
		if($token){
			$userArr = explode(',',auth_code($token,'DECODE'));

			$data['uid'] = $userArr[0];
		}
        $data['name'] = request()->param('name','','trim') ;
        $data['file'] = str_replace($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'],'',request()->param('file','','trim')) ;
        $data['content'] = request() ->param('content','','trim');
        $data['credit_type'] = request()->param('credit_type',1,'intval') ;
        $data['characteristic'] = request()->param('characteristic','','trim') ;

        $file_screenshot = '';
        if (isset($param['file'])) {
            $imgcontent = $param['file'][0]["content"];
            $screenFile = base64_image_content($imgcontent, "./upload/image");
            if ($screenFile) {
                $file_screenshot = $screenFile;
            }else{
                return ['code'=>0,'code_dec'=>config($lang.'.lang2')[2]];
            }
        }

        $voucher_screenshot = '';
        if (isset($param['voucher'])) {
            $imgcontent = $param['voucher'][0]["content"];
            $screenFile = base64_image_content($imgcontent, "./upload/image");
            if ($screenFile) {
                $voucher_screenshot = $screenFile;
            }else{
                return ['code'=>0,'code_dec'=>config($lang.'.lang2')[2]];
            }
        }
        $data['file'] = $file_screenshot;
        $data['voucher'] = $voucher_screenshot;
        //数据验证

		$validate = validate('app\api\validate\Nft');
		if(!$validate->scene('add')->check($data)){
			//抛出异常
			return json(['code'=>0,'code_dec'=>$validate->getError()]);
		}


        $data['ordersn'] = date('YmdHis').rand(0,9).rand(0,9).rand(0,9).rand(0,9) ;
        $data['status'] =2;
        $data['state'] =1;
        $data['from'] =2;
        $data['create_time'] =time();

		$res = model('Nft')->save($data);
		return json(['code'=>1,'data'=>[],'code_dec'=>config($lang.'.lang2')[3]]);
	}
    /**
	*
    *转出nft
	**/
	public function export(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
		$token			= request()->param('token','','trim') ;
        $nft_id=request()->param('nft_id','','trim') ;
        $data['uid']=0;
		if($token){
			$userArr = explode(',',auth_code($token,'DECODE'));

			$data['uid'] = $userArr[0];
		}
		if(!$nft_id){
			return ['code'=>0,'code_dec'=>config($lang.'.lang2')[1]];
		}
		$info=db('nft')->where(['id'=>$nft_id,'uid'=>$data['uid'],'status'=>1,'state'=>1,'is_move'=>0])->find();
		if(!$info){
			return ['code'=>0,'code_dec'=>config($lang.'.lang2')[1]];
		}
        $data['ordersn'] = date('YmdHis').rand(0,9).rand(0,9).rand(0,9).rand(0,9) ;
        $data['remarks'] = request()->param('remarks','','trim') ;
		$data['address'] = request() ->param('address','','trim');
        $data['status'] = 1;
		$data['nft_id'] = $nft_id;
        $data['credit_type'] = request()->param('credit_type',1,'intval') ;
        $data['create_time'] = time() ;
        //数据验证

//		$validate = validate('app\api\validate\Nft');
//		if(!$validate->scene('export')->check($data)){
//			//抛出异常
//			return json(['code'=>0,'code_dec'=>$validate->getError()]);
//		}
		$res = db('nft_ex')->insert($data);
//		db('nft')->where(['id'=>$nft_id])->update(['is_move'=>1]);
		return json(['code'=>1,'data'=>[],'code_dec'=>config($lang.'.lang2')[3]]);
	}

    public function exportList(){
        $param		= input('post.');
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
        $token			= (input('post.token')) ? input('post.token') : '';
        //每页记录数
        $pageSize	= (isset($param['pagesize']) and $param['pagesize']) ? $param['pagesize'] : 10;
        //当前页
        $page		= (isset($param['page']) and $param['page']) ? $param['page'] : 1;
        $where = '';

        $data	= db('nft_ex')->alias('a')
            ->field('a.*,nft.name')
            ->leftJoin('nft',"nft.id=a.nft_id")
            ->where($where)
            ->order('create_time desc')
            ->paginate($pageSize)
            ->toArray();

        foreach ($data['data'] as $key => &$value) {
            $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
        }
        return json(['code'=>1,'data'=>$data]);
    }
    /**
	*
    *合成nft
	**/
	public function compound(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
		$debri_id=request()->param('id','','trim') ;
		$token			= request()->param('token','','trim') ;

        $data['uid']=0;
		if($token){
			$userArr = explode(',',auth_code($token,'DECODE'));

			$data['uid'] = $userArr[0];
		}
		if(!$debri_id){
			return ['code'=>0,'code_dec'=>config($lang.'.lang2')[4]];
		}
		$debInfo=db('users_goods')->alias('a')
		->field("a.id,a.nft_id,a.available_number,b.name,b.file,b.credit_type,b.characteristic,b.content,b.total_number,b.recovery_price,b.release_price,b.reward_price,a.compound_num")
		->join('nft b','a.nft_id=b.id','left')
		->where("a.status=1 and a.uid={$data['uid']} and a.id=$debri_id")
		->find();
		if(!$debInfo){
			return json(['code'=>0,'code_dec'=>config($lang.'.lang2')[4]]);
		}

		if($debInfo['available_number']<$debInfo['compound_num']){
			return json(['code'=>0,'code_dec'=>config($lang.'.lang2')[5]]);
		}
        $data['name'] = $debInfo['name'];
		$data['file'] = $debInfo['file'];
		$data['content'] = $debInfo['content'];
		$data['characteristic'] = $debInfo['characteristic'];
		$data['uid'] = $data['uid'];
		$data['create_time'] = time();
		$data['pid'] = $debInfo['nft_id'];
		$data['from'] = 3;
		$data['status'] = 1;
		$data['recovery_price'] = $debInfo['recovery_price'];
		$data['release_price'] = $debInfo['release_price'];
		$data['reward_price'] = $debInfo['reward_price'];
		$data['credit_type'] = $debInfo['credit_type'];
		$data['ordersn'] = date('YmdHis').rand(0,9).rand(0,9).rand(0,9).rand(0,9) ;
		$res = model('Nft')->save($data);

		//把原有的碎片清空掉
		db('users_goods')->where('id', $debri_id)->Dec('available_number', $debInfo['compound_num'])->update();
		db('users_goods')->where('id', $debri_id)->Dec('total_number', $debInfo['compound_num'])->update();
		if($debInfo['total_number']<=$debInfo['compound_num']){
			db('users_goods')->where('id', $debri_id)->update(['status'=>2]);
		}
		return json(['code'=>1,'data'=>[],'code_dec'=>config($lang.'.lang2')[3]]);
	}
    /**
	*
    *拆分nft成碎片
	**/
	public function split(){
		$data = model('UserVip')->userBuyVip();
		return json($data);
	}




}
